package com.neusoft.license.generate.handler;

import com.neusoft.license.generate.constant.ExceptionConstant;
import com.neusoft.license.common.constant.RestConstant;
import com.neusoft.license.common.exception.BaseException;
import com.neusoft.license.common.res.ErrorRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: xlh
 * @Description: 全局异常处理器
 * @Date: Create in 17:04 2020/8/24 0024
 */
@Slf4j
@RestControllerAdvice
public class GlobalDefaultExceptionHandler {
    /**
     * 处理自定义异常信息
     * @param e
     * @return RestErrorResponse
     */
    @ExceptionHandler(BaseException.class)
    public ErrorRestResponse UnProccessableExceptin(BaseException e) {
        log.error(ExceptionConstant.USERS_DEFINED_EXCEPTION, e);
        return new ErrorRestResponse().error(e.getCode(), e.getMessage());
    }


    /**
     * 处理系统异常
     * @param e
     * @return RestErrorResponse
     */
    @ExceptionHandler(Exception.class)
    public ErrorRestResponse interceptRunTimeException(Exception e) {
        log.error(ExceptionConstant.SYSTEM_EXCEPTION, e);
        StringBuilder builder = new StringBuilder();
        builder.append(e.getMessage());
        return new ErrorRestResponse().error(RestConstant.ERROR_SERVER_CODE, builder.toString());
    }
}
